<template>
  <div class="box">
    <div class="login">
      <div class="lt">
        <div class="lt_t">
          <img src="../../assets/images/login_logo.png" alt="logo">
        </div>
        <div class="lt_b">
          <img src="../../assets/images/login_img.png">
        </div>
      </div>
      <div class="rt">
        <div class="rt_f">
          <div class="tit_1">圣东OA办公系统</div>
          <div class="tit_2">让工作更高效的完成</div>
          <div class="rt_l">
            <el-form class="login-form" :rules="ruleData" ref="form" :model="formData" label-width="80px">
              <el-form-item prop="userName" label="用户名">
                <el-input v-model="formData.userName" placeholder="请输入用户名">
                  <i slot="prefix" class="fa fa-user-o"></i>
                </el-input>
              </el-form-item>
              <el-form-item prop="passWord" label="密码">
                <el-input type="passWord" v-model="formData.passWord" show-password placeholder="请输入用登录密码">
                  <i slot="prefix" class="fa fa-key"></i>
                </el-input>
              </el-form-item>
              <el-form-item prop="name" label="姓名">
                <el-input v-model="formData.name" placeholder="请输入姓名">
                  <i slot="prefix" class="fa fa-address-card-o"></i>
                </el-input>
              </el-form-item>
              <el-form-item prop="phone" label="电话">
                <el-input v-model="formData.phone" placeholder="请输入电话">
                  <i slot="prefix" class="fa fa-phone"></i>
                </el-input>
              </el-form-item>
              <!-- <el-form-item prop="number" label="工号">
                <el-input v-model="formData.number" placeholder="请输入工号">
                  <i slot="prefix" class="fa fa-list-ol"></i>
                </el-input>
              </el-form-item> -->
              <el-form-item prop="email" label="邮箱">
                <el-input v-model="formData.email" placeholder="请输入邮箱">
                  <i slot="prefix" class="fa fa-envelope-o"></i>
                </el-input>
              </el-form-item>
              <el-form-item class="login-btn">
                <el-button type="primary" @click="submit" round>注册</el-button>
                <el-button type="info" @click="cancel" round>返回</el-button>
              </el-form-item>
            </el-form>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import swal from 'sweetalert'
import _ from 'underscore'
import { Register } from '@/api/api'
export default {
  data () {
    var checkName = (rule, value, callback) => {
      if (!value) {
        return callback(new Error('请输入姓名'))
      } else {
        let patt = /^[\u4e00-\u9fa5]{2,4}$/
        if (!patt.test(value)) {
          callback(new Error('请输入正确的姓名格式'))
        } else {
          callback()
        }
      }
    }
    var checkPass = (rule, value, callback) => {
      if (!value) {
        return callback(new Error('请输入密码'))
      } else {
        let patt = /^[A-Za-z0-9_.?,;-]+$/
        if (!patt.test(value)) {
          callback(new Error('请输入正确的字母（区分大小写），数字和.?,;-（英文格式的符号）'))
        } else {
          callback()
        }
      }
    }
    var checkphone = (rule, value, callback) => {
      if (!value) {
        return callback(new Error('请输入手机号'))
      } else {
        let patt = /^1[345789]\d{9}$/
        if (!patt.test(value)) {
          callback(new Error('请输入正确的手机格式'))
        } else {
          callback()
        }
      }
    }
    var checkNo = (rule, value, callback) => {
      if (!value) {
        return callback(new Error('请输入工号'))
      } else {
        let patt = /^[0-9]*$/
        if (!patt.test(value)) {
          callback(new Error('请输入只能是数字格式'))
        } else {
          callback()
        }
      }
    }
    var checkEmail = (rule, value, callback) => {
      if (!value) {
        return callback(new Error('请输入邮箱'))
      } else {
        let patt = /^[a-zA-Z0-9]+([-_.][a-zA-Z0-9]+)*@[a-zA-Z0-9]+([-_.][a-zA-Z0-9]+)*\.[a-z]{2,}$/
        if (!patt.test(value)) {
          callback(new Error('请输入正确的邮箱格式'))
        } else {
          callback()
        }
      }
    }
    return {
      formData: {
        userName: '',
        passWord: '',
        name: '',
        phone: '',
        number: '',
        email: ''
      },
      ruleData: {
        userName: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
        passWord: [{ required: true, validator: checkPass, trigger: 'blur' }],
        name: [{ required: true, validator: checkName, trigger: 'blur' }],
        phone: [{ required: true, validator: checkphone, trigger: 'blur' }],
        number: [{ required: true, validator: checkNo, trigger: 'blur' }],
        email: [{ required: true, validator: checkEmail, trigger: 'blur' }]
      }
    }
  },
  methods: {
    submit: _.debounce(function () {
      let _this = this
      this.$refs.form.validate((valid) => {
        if (!valid) return false
        let params = {
          username: this.formData.userName,
          password: this.formData.passWord,
          name: this.formData.name,
          phone: this.formData.phone,
          createTime: new Date(),
          number: this.formData.number,
          email: this.formData.email,
        }
        Register(params)
          .then((res) => {
            // console.log(res)
            if (res.code == 200) {
              this.$refs.form.resetFields()
              swal('成功', '注册成功，请等待管理员审核', 'success').then(
                function () {
                  _this.$router.replace({ name: 'login' })
                }
              )
            } else {
              swal('错误', res.message, 'warning')
            }
          })
          .catch((err) => {
            swal('错误', '网络连接异常', 'error')
          })
      })
    }, 800),
    cancel () {
      this.$router.go(-1)
    },
  }
}
</script>
<style lang="less" scoped>
.box {
  width: 100%;
  min-width: 1200px;
  height: 100%;
  background: url('../../assets/images/login_bg.png') no-repeat;
  background-size: 100% 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  position: relative;
}
.login {
  width: 74.8%;
  min-width: 1000px;
  height: 750px;
  background: url('../../assets/images/login_form.png') no-repeat;
  background-size: 100% 100%;
  display: flex;
  .lt {
    width: 60%;
    height: 100%;
    .lt_t {
      width: 100%;
      height: 200px;
      img {
        width: 190px;
        height: 118px;
        margin-top: 56px;
        margin-left: 110px;
      }
    }
    .lt_b {
      width: 100%;
      height: 550px;
      display: flex;
      justify-content: center;
      align-items: center;
      img {
        display: inline-block;
        width: 522px;
        height: 424px;
      }
    }
  }
  .rt {
    width: 40%;
    height: 100%;
    display: flex;
    align-items: center;
    .rt_f {
      width: 80%;
      height: 80%;
      .tit_1 {
        font-size: 38px;
        font-weight: bold;
      }
      .tit_2 {
        margin-top: 12px;
        color: #c0c4cc;
      }
      .rt_l {
        margin-top: 50px;
      }
    }
  }
}
.login-form {
  position: relative;
  top: 20px;
}
.login-btn {
  margin-top: 30px;
  /deep/ .el-button {
    width: 40%;
  }
}
.rt_l /deep/ .el-input__inner {
  border: 0;
  border-bottom: 1px solid #c0c4cc;
  border-radius: 0;
}
</style>